package com.fc.h5dispaly.h5display.hotfix;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import static com.fc.h5dispaly.h5display.hotfix.FixDexUtils.DEX_DIR;

/**
 * Created by fc on 2020/4/17
 * Describe:
 * @author fc
 */
public class FixTestActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        setContentView(layout,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        Button testBtn = new Button(this);
        testBtn.setText("测试");
        layout.addView(testBtn);
        testBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TestCrash crash = new TestCrash();
                crash.testFix(FixTestActivity.this);
            }
        });

        final Button fixBtn = new Button(this);
        fixBtn.setText("修复");
        layout.addView(fixBtn);
        fixBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fixBug();
            }
        });
    }


    private void fixBug() {
        //目录 data/data/packageName/odex
        File fileDir = getDir(DEX_DIR, Context.MODE_PRIVATE);
        //该目录下放置修复好的dex文件
        String name = "classes2.dex";
        String filePath = fileDir.getAbsolutePath() + File.separator + name;
        Log.d("FcHotFix", "fixBug: " + filePath);
        Log.d("FcHotFix", "FileInputStream: "+Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+name);
        File file = new File(filePath);
        if(file.exists()){
            file.delete();
        }
        //搬家：把下载好的在sd卡里面的修复了的classes2.dex复制到应用目录
        InputStream is = null;
        FileOutputStream os = null;
        try {
            //复制并粘贴文件
            is = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+name);
            os = new FileOutputStream(filePath);
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((len = is.read(buffer)) != -1){
                os.write(buffer,0,len);
            }

            //粘贴完文件
            File f = new File(filePath);
            if(f.exists()){//文件从sk卡赋值到应用运行目录下，成功则toast提示
                Toast.makeText(this,"dex重写成功",Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
